package com.ufgov.agcfs.taskPreset.service;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ufgov.agcfs.taskPreset.dao.AccBookDao;
import com.ufgov.agcfs.taskPreset.dao.AssetCardDao;
import com.ufgov.agcfs.taskPreset.dao.GlDefDao;
import com.ufgov.agcfs.taskPreset.dao.IpCompanyDao;

@Service
public class OutPutExcelService {
	@Autowired
	private AccBookDao accBookDao;
	@Autowired
	private AssetCardDao assetCardDao;
	@Autowired
	private GlDefDao glDefDao;
	@Autowired
	private IpCompanyDao ipCompanyDao;
	
	private FileOutputStream fos;
	
	@SuppressWarnings("unchecked")
	public Boolean outPutExcel(String nd,String co_code,String parent_co_id) throws IOException{
		HSSFWorkbook workBook=null;
		String[] title={"单位代码","单位名称","上级单位代码","公共档案","总账数据","固定资产数据"};
		//参数定义
		Map<String, Object> param=new HashMap<String, Object>();
		param.put("co_code", co_code);
		param.put("parent_co_id", parent_co_id);
		//获取单位代码、单位名称、上级单位代码
		List<?> dwList =ipCompanyDao.selectInfoByPCoId(param);
		//根据dwList查询其他表数据
		List<String> data=new ArrayList<String>();
		for(int i=0;i<dwList.size();i++){
			data.add((((Map<String,Object>) dwList.get(i)).get("CO_CODE")).toString());
			data.add((((Map<String,Object>) dwList.get(i)).get("CO_NAME")).toString());
			data.add((((Map<String,Object>) dwList.get(i)).get("PARENT_CO_ID")).toString());
			//参数定义
			Map<String, Object> param2=new HashMap<String, Object>();
			param2.put("nd", nd);
			param2.put("dw_code",(((Map<String,Object>) dwList.get(i)).get("CO_CODE")).toString());
			if(accBookDao.selectAccBookNo(param2).equals("")){
				data.add("无");
			}else{
				data.add("已导入");
			}
			if(glDefDao.selectAccBookNo(param2).equals("")){
				data.add("无");
			}else{
				data.add("已导入");
			}
			if(assetCardDao.selectAssetNo(param2).equals("")){
				data.add("无");
			}else{
				data.add("已导入");
			}
		}
		
		try{
			//导出地址
			fos=new FileOutputStream("D://"+nd+"导出数据情况"+".xls");
			workBook=new HSSFWorkbook();   //创建Excel
			HSSFSheet sheet=workBook.createSheet("导出数据情况");     //工作表
			HSSFRow row=sheet.createRow(0);        //行
			HSSFCell cell=null;     //列
			//第一行输入标题
			for(int j=0;j<title.length;j++){
				cell=row.createCell(j);
				cell.setCellValue(new HSSFRichTextString(title[j]));
			}
			//写入数据到excel
			for(int t=0;t<data.size()/6;t++){
				row=sheet.createRow(t+1);
				for(int k=0;k<6;k++){
					cell=row.createCell(k);
					cell.setCellValue(new HSSFRichTextString(data.get(t*6+k)));
				}
			}
			//写入本地
			workBook.write(fos);
			fos.flush();
			fos.close();
			return true;
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			fos.flush();
			fos.close();
		}
		return false;
	}
}
